<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->

    <el-card>
      <!-- 头部区域 -->
      <el-row :gutter="20">
        <!-- 搜索框 -->

        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchBycontent"
            ></el-button>
          </el-input>
        </el-col>

        <!-- 添加按钮 -->

        <el-col :span="4">
          <el-button type="primary" @click="gotoadd">添加</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->

      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>

        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>

        <el-table-column prop="goods_price" label="商品价格(元)" width="100px">
        </el-table-column>

        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>

        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatTime }}
          </template>
        </el-table-column>

        <!-- 操作 -->

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->

            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="gotoEdit(scope.row.goods_id)"
            ></el-button>

            <!-- 删除按钮 -->

            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="delgoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getdata } from "../../network/getdata";
import { formatTime } from "../../common/utils/formdate";
export default {
  name: "list",
  data() {
    return {
      //商品列表查询条件对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 总条数
      total: 0,
      // 商品数据列表
      goodsList: [],
      //根据 id 查询出来的商品信息
      goodsInfo: {},
    };
  },
  filters: {
    //格式化时间
    formatTime(time) {
      return formatTime(time * 1000, "Y-M-D h:m:s");
    },
  },
  methods: {
    //跳转到商品编辑页面
    gotoEdit(goods_id) {
      //先获取商品信息
      getdata({
        method: "get",
        url: `goods/${goods_id}`,
      }).then((result) => {
        const res = result.data;
        if (res.meta.status !== 200) {
          this.$message.error("获取商品信息失败");
          return;
        }
        this.goodsInfo = res.data;
        console.log(this.goodsInfo);
        this.$router.push({
          name: "edit",
          params: this.goodsInfo,
        });
      });
    },
    // 跳转到添加页面
    gotoadd() {
      this.$router.push("/good/add");
    },
    // 搜索
    searchBycontent() {
      this.getGoodsList();
    },
    //删除商品
    delgoods(id) {
      this.$confirm("此操作会永久删除该商品,是否继续", "温馨提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确定
          getdata({
            method: "delete",
            url: `/goods/${id}`,
          }).then((result) => {
            const res = result.data;
            if (res.meta.status !== 200) {
              //删除失败
              this.$message.error("删除失败");
              return;
            }

            this.$message.success("删除成功");
            this.getGoodsList();
          });
        })
        .catch((err) => {
          this.$message.info("已取消操作");
        });
    },
    //监听当前页码改变的事件
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum;
      this.getGoodsList();
    },
    //监听当前页码条数改变事件
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getGoodsList();
    },
    //获取商品列表
    getGoodsList() {
      getdata({
        method: "get",
        url: `/goods`,
        params: this.queryInfo,
      }).then((result) => {
        const res = result.data;
        if (res.meta.status !== 200) {
          this.$message.error("获取商品列表失败");
          return;
        }
        //获取成功
        this.goodsList = res.data.goods;
        console.log(this.goodsList);
        this.total = res.data.total;
      });
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
  font-size: 12px;
}
.el-pagination {
  margin-top: 15px;
}
</style>